package customer

import (
	"context"
	"github.com/google/wire"
	"sieve_admin_server_userpass/app/api/util"
	"sieve_admin_server_userpass/app/contextx"
	"sieve_admin_server_userpass/app/schema"
	"sieve_admin_server_userpass/app/server"
	serviceCustomer "sieve_admin_server_userpass/app/service/customer"
)

var (
	_               util.API = (*AnnouncementAPI)(nil)
	AnnouncementSet          = wire.NewSet(wire.Struct(new(AnnouncementAPI), "*"))
)

type AnnouncementAPI struct {
	util.EmptyAPI
	AnnouncementSrv *serviceCustomer.AnnouncementSrv
}

func (a *AnnouncementAPI) GetApis() []*server.BatchModTree {
	return []*server.BatchModTree{
		{
			Obj:   a,
			Name:  "announcement",
			Title: "公告管理", Perms: []*server.Perm{
				{Name: "query", Title: "搜索"},
				{Name: "add", Title: "添加"},
				{Name: "del", Title: "删除"},
				{Name: "edit", Title: "编辑"},
				{Name: "enable", Title: "启用"},
				{Name: "disable", Title: "禁用"},
			},
			Order: 1,
		},
	}
}

func (a *AnnouncementAPI) API_add_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("title"),
				contextx.NewVar("content"),
				contextx.NewVar("role_id", true),
			},
		)
	)

	return a.AnnouncementSrv.Create(context.Background(), reqArgs)
}

func (a *AnnouncementAPI) API_query_list(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("title"),
			},
		)

		p = schema.PaginationParam{
			PageIndex: util.ToInt(ctx.Args.Get("page_index")),
			PageSize:  util.ToInt(ctx.Args.Get("page_size")),
		}
		repKeys = []string{"*"}
	)

	return a.AnnouncementSrv.Query(context.Background(), reqArgs, p, repKeys)
}

func (a *AnnouncementAPI) API_edit_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
				contextx.NewVar("title"),
				contextx.NewVar("content"),
			},
		)
	)

	return a.AnnouncementSrv.Update(context.Background(), reqArgs)
}

func (a *AnnouncementAPI) API_enable_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
			},
		)
	)

	return a.AnnouncementSrv.Enable(context.Background(), reqArgs)
}

func (a *AnnouncementAPI) API_disable_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
			},
		)
	)

	return a.AnnouncementSrv.Disable(context.Background(), reqArgs)
}

func (a *AnnouncementAPI) API_del_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
			},
		)
	)

	return a.AnnouncementSrv.Delete(context.Background(), reqArgs)
}
